package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.park_management.entity.Campaignparticipants_wjs;
import com.example.park_management.entity.FaceEngineTest;
import com.example.park_management.entity.MarketingCampaigns_wjs;
import com.example.park_management.service.CampaignparticipantsService_wjs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/AboutMarketing")
public class CampaignparticipantsController_wjs {
    @Autowired
    private CampaignparticipantsService_wjs cpservice_wjs;

    @GetMapping
    public List<Campaignparticipants_wjs> index() {
        return cpservice_wjs.list();
    }

    @GetMapping("/seletebyTenantID")
    public List<Integer> seletebyTenantID(Integer tenantID) {
        return cpservice_wjs.seletebyTenantID(tenantID);
    }

    @CrossOrigin(origins = "http://localhost:8081/AboutMarketing")
    @PostMapping("/checkFace")
    public boolean checkFace(@RequestBody Map<String, String> requestBody) {
        String photoData = requestBody.get("photo");
        // 截取Base64编码数据中的实际图片数据部分（去掉 data:image/jpeg;base64, 的部分）
        String base64Image = photoData.split(",")[1];

        // 将Base64编码字符串解码为字节数组
        byte[] decodedBytes = Base64.getDecoder().decode(base64Image);

        // 设置保存图片的路径和文件名
        String filePath = "E:/springboot_learning/park-management/checkFace/image.jpg";

        try {
            // 将解码后的字节数组写入文件
            FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));
            fileOutputStream.write(decodedBytes);
            fileOutputStream.close();

            FaceEngineTest faceEngineTest = new FaceEngineTest();
//            boolean result = true;
            boolean result = faceEngineTest.checkFace();
            System.out.println(result);
            File fileToDelete = new File(filePath);
            fileToDelete.delete();

            return result;
        } catch (IOException e) {
            System.out.println("保存图片时出错：" + e.getMessage());
            return false;
        }
    }

    @PostMapping
    public boolean savaMC_wjs(@RequestBody Campaignparticipants_wjs cp_wjs) {
        System.out.println("ajsdgjasgdhasgdh");
        System.out.println(cp_wjs);
        return cpservice_wjs.savacpwjs(cp_wjs);
    }

    @DeleteMapping("/{id}")
    public boolean delete_MC(@PathVariable Integer id) {
        return cpservice_wjs.removeById(id);
    }


    @DeleteMapping("/deletebyCID_TID")
    public boolean deletebyCID_TID(@RequestParam Integer CID, @RequestParam Integer TID) {
        System.out.println(CID);
        System.out.println(TID);
        return cpservice_wjs.removeByCID_TID(CID, TID);
    }



    @GetMapping("/page")
    public IPage<Campaignparticipants_wjs> findPage(@RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize,
                                                  @RequestParam(defaultValue = "") String campaignID,
                                                  @RequestParam(defaultValue = "") String tenantID) {
        IPage<Campaignparticipants_wjs> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Campaignparticipants_wjs> queryWrapper = new QueryWrapper<>();
        if (!"".equals(campaignID)) {
            queryWrapper.like("campaignID", campaignID);
        }
        if (!"".equals(tenantID)) {
            queryWrapper.like("tenantID", tenantID);
        }
//        queryWrapper.or().like("")
        queryWrapper.orderByDesc("campaignID");
        return cpservice_wjs.page(page,queryWrapper);
    }
}
